package xiaoze.veriface.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xiaoze.veriface.constant.PropertiesConstant;
import xiaoze.veriface.http.Response;
import xiaoze.veriface.utils.FaceUtils;
import xiaoze.veriface.utils.Utils;
import java.io.IOException;
/**
 * FaceController
 * @author xiaoze
 * @date 2018/7/14
 * 用谷歌或火狐浏览器
 * Api网址：https://console.faceplusplus.com.cn/documents/4888373
 */
@Controller
@RequestMapping(value="/face")
public class FaceController {

    /**
     * 进入人脸检测界面
     * @return String
     */
    @GetMapping("/toDetect")
    public String toDetect() {
        return "/html/face/detect_face.html";
    }

    /**
     * 人脸各特征实现
     * @return String
     */
    @RequestMapping("/detect")
    @ResponseBody
    public String getFace(String imgString) throws Exception {

        FaceUtils faceUtils = new FaceUtils();

        byte[] buff = FaceUtils.getStringImage(FaceUtils.getBase64String(imgString));

        Response response = faceUtils.getCheckResponse(buff) ;
        String str = "";

        if(response != null){
            str = new String(response.getContent());
        }

        return str;
    }

    /**
     * 进入人脸检测界面
     * @return String
     */
    @GetMapping("/toContrast")
    public String toContrast() {
        return "/html/face/contrast_face.html";
    }

    /**
     * 人脸对比方法
     * @return String
     */
    @RequestMapping("/contrast")
    @ResponseBody
    public String contrast(String imgString) throws IOException {

        String strOne = "";
        String strTwo = "";

        String[] sOne;
        String[] sTwo;

        Integer sLengthOne;
        Integer sLengthTwo;

        String faceTokenOne;
        String faceTokenTwo;
        int j=0;

        FaceUtils faceUtils = new FaceUtils();

        byte[] buff = FaceUtils.getStringImage(FaceUtils.getBase64String(imgString));

        Response response = faceUtils.getCheckResponse(buff) ;

        if(response != null){
            strOne = new String(response.getContent());
        }
        sOne=strOne.split("\"");

        //解决一张图片有多个人问题
        for(String str:sOne){
            if(PropertiesConstant.AGE.equals(str)) {
                j++;
            }
            if(j>1) {
                return "对不起，您上传的用户头像照片质量不达标(即不是单个有效头像)，请重新上传！";
            }
        }
        sLengthOne = sOne.length;
        faceTokenOne = sOne[sLengthOne-2];

        int oneNum = 4;
        if(!PropertiesConstant.FACE_TOKEN.equals(sOne[sLengthOne-oneNum])) {
            return "对不起，您上传的用户头像照片质量不达标(即不是单个有效头像)，请重新上传！";
        }

        String imageLocalPath = PropertiesConstant.IMAGE_LOCAL_PATH;
        Utils utils = new Utils();

        Response response1 = faceUtils.getCheckResponse(utils.getBytesFromFile(imageLocalPath)) ;
        if(response1 != null){
            strTwo = new String(response1.getContent());
        }

        sTwo=strTwo.split("\"");
        sLengthTwo = sTwo.length;
        faceTokenTwo = sTwo[sLengthTwo-2];

        String compareInfo = "" ;
        String[] compareInfoStr;

        Response response2 = faceUtils.getCompareResponse(faceTokenOne,faceTokenTwo);
        if(response2 != null){
            compareInfo = new String(response2.getContent());
        }
        compareInfoStr = compareInfo.split("\"");

        Double likeInfo = Double.parseDouble(compareInfoStr[2].substring(2, compareInfoStr[2].length()-2));
        if(likeInfo < PropertiesConstant.FACE_NUM) {
            return "认证不成功，请本人刷脸";
        }
        return "认证成功";
    }

}
